import { showLoading, hideLoading } from '@/api/loading';
const BaseFilter = {
  request(servObj: any) {
    servObj.interceptors.request.use(
      (config: any) => {
        config.headers['Content-Type'] = 'application/json;charset=UTF-8';
        showLoading();
        return config;
      },
      (error: any) => {
        hideLoading();
        return Promise.reject(error);
      },
    );
  },
  response(servObj: any) {
    servObj.interceptors.response.use(
      (res: any) => {
        hideLoading();
        if (res.status === 200 && res.data) {
          return res.data;
        } else {
          return Promise.reject(new Error('未知服务器错误'));
        }
      },
      (error: any) => {
        hideLoading();
        return Promise.reject(error);
      },
    );
  },
};

function requireFilter(servObj: any) {
  BaseFilter.request(servObj);
  BaseFilter.response(servObj);
}

export default requireFilter;
